package imaa.framework.configuration;

import java.util.EnumSet;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.DispatcherServlet;

public class Initializer implements WebApplicationInitializer {
	
	private static final String DISPATCHER_SERVLET_NAME = "dispatcher";
	
	 public void onStartup(ServletContext servletContext) throws ServletException {  
	        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();  
	        ctx.register(WebConfiguration.class);  
	          
	        ctx.setServletContext(servletContext);        
	  
	        registerHiddenHttpMethodFilter(servletContext);   
	          
	        Dynamic servlet = servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(ctx));  
	        servlet.addMapping("/api/*");  
	        servlet.setLoadOnStartup(1);  
	          
	    }  
	      
	    private void registerHiddenHttpMethodFilter(ServletContext servletContext) {  
	        FilterRegistration.Dynamic fr = servletContext  
	                .addFilter("hiddenHttpMethodFilter", HiddenHttpMethodFilter.class);  
	        fr.addMappingForServletNames(  
	                EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD),   
	                false,   
	                DISPATCHER_SERVLET_NAME);  
	    }

}
